package form;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;

import bean.Patient;
import bean.User;

import rmi.ServeurRmiInterface;

import annotation.*;

public class Valider {
	public static boolean form(HttpServletRequest request, Object o) {
		boolean res = true;
		try {
			Class c = o.getClass();
			Field[] fields = c.getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				if (fields[i].isAnnotationPresent(Form.class)) {
					String val = request.getParameter(
							fields[i].getName());
					if (val == null) val = "";
					
					if (fields[i].isAnnotationPresent(Req.class)) {
						if (val.trim().equals("") && fields[i].getName() != "dateNaissance") {
							Method getErr = c.getMethod("getErr");
							Hashtable <String,String> err = 
								(Hashtable <String,String>)getErr.invoke(o);
							Req ann = (Req)fields[i].getAnnotation(Req.class);
							err.put(fields[i].getName(),ann.mess());
							res= false;
						}
					}
					
					String field = fields[i].getName();
					String nomSetter = "set" + field.substring(0,1).toUpperCase() + field.substring(1);
					if(fields[i].getName() == "dateNaissance" && !request.getParameter("jour").equals("--") && !request.getParameter("mois").equals("--") && !request.getParameter("annee").equals("----")){
						Patient p = (Patient) o;
						p.setJour(Integer.parseInt(request.getParameter("jour")));
						p.setMois(Integer.parseInt(request.getParameter("mois")));
						p.setAnnee(Integer.parseInt(request.getParameter("annee")));
						p.createDate();
					}else if(fields[i].getName() == "dateNaissance" && (request.getParameter("jour").equals("--") || request.getParameter("mois").equals("--") || request.getParameter("annee").equals("----"))){
						Method getErr = c.getMethod("getErr");
						Hashtable <String,String> err = 
							(Hashtable <String,String>)getErr.invoke(o);
						Req ann = (Req)fields[i].getAnnotation(Req.class);
						err.put(fields[i].getName(),ann.mess());
					}else if (fields[i].getType() == String.class) {
						Method mSetter = c.getMethod(nomSetter,String.class);
						mSetter.invoke(o,val);
						System.out.println("val = "+val);
					}else if(fields[i].getType() == int.class){
						Method mSetter = c.getMethod(nomSetter,int.class);
						mSetter.invoke(o,Integer.parseInt(val));
					}else if(fields[i].getType() == float.class){
						Method mSetter = c.getMethod(nomSetter,float.class);
						mSetter.invoke(o,Float.parseFloat(val));
					}else{
						//System.out.println(fields[i].getType() == int.class);
						System.out.println("Valider.form: type inconnu pour "+field);
					}
				}
			}
		} catch (Exception e) {
			res = false;
			System.out
					.println("Valider.form: erreur [" + e.getMessage() + "]");
		}
		return res;
	}
	
	public static boolean isLog(String login, String password) {
		int port = 54000;
		User user = new User();
		user.setLogin(login);
		user.setPassword(password);
		try {
			Registry registry = LocateRegistry.getRegistry(port);
			ServeurRmiInterface serveur = (ServeurRmiInterface) registry.lookup("ServeurRMI");
			return serveur.isLog(user);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}